/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * http://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */
package com.dian.modules.base.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.dian.common.vo.TreeVo;
import com.dian.common.exception.RRException;
import com.dian.common.validator.ValidatorUtils;
import com.dian.common.validator.group.AddGroup;
import com.dian.common.validator.group.UpdateGroup;
import  com.dian.common.enums.ValidEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dian.common.utils.PageUtils;
import com.dian.common.utils.Query;
import com.dian.common.server.CommonService;
import com.dian.modules.base.dao.ClassDao;
import com.dian.modules.base.entity.ClassEntity;
import com.dian.modules.base.service.ClassService;

/**
 * 分类表服务实现类
 *
 * @author Jinag Run
 * @email 1685269193qq.com
 * @date 2019-05-22 23:47:42
 */
@Service("baseClassService")
public class ClassServiceImpl extends ServiceImpl<ClassDao, ClassEntity> implements ClassService {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    public CommonService commonService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<ClassEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        IPage<ClassEntity> page = this.page(new Query<ClassEntity>().getPage(params),queryWrapper );

        return new PageUtils(page);
    }
    @Override
    public boolean saveInfo(ClassEntity baseClassEntity) {

        baseClassEntity.setClassCode(commonService.getCode(ClassEntity.class));
        this.checkParams(baseClassEntity,AddGroup.class);

        //保存
        this.save(baseClassEntity);
        return true;
    }


    @Override
    public boolean updateInfo(ClassEntity baseClassEntity) {



        //数据完整性校验
        this.checkParams(baseClassEntity, UpdateGroup.class);

        //更新
        this.updateById(baseClassEntity);

        return true;
    }



    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteInfo(Long id){
        //删除校验
        this.deleteCheckState(id);
        this.removeById(id);
        return true;
    }

    @Override
    public List<TreeVo<ClassEntity>> queryTreeAll(Map<String, Object> params) {
        TreeVo<ClassEntity> treeVo=new TreeVo<>();
        treeVo.setChildren(getChilds(0L));
        treeVo.setId(0L);
        treeVo.setTitle("分类");
        treeVo.setSpread(true);
        return   CollUtil.newArrayList(treeVo);
    }


    @Override
    public List<ClassEntity> selectInfo() {
        List<ClassEntity> classEntity = list();
        ClassEntity root = new ClassEntity();
        root.setId(0L);
        root.setClassName("一级分类");
        root.setParentId(-1L);
        classEntity.add(root);
        return   classEntity;
    }



    /***********************************************************************************************/
    /****************************************** 私有方法 ********************************************/
    /***********************************************************************************************/
    /**
     * 删除状态校验
     *
     * @param
     */
    private void deleteCheckState(Long id) {
        int nCount=this.baseMapper.queryChildCount(id);
        if(nCount>0){
            throw new RRException(String.format("请先删除子数据"));
        }

    }


    /**
     * 参数校验
     * @param baseClassEntity
     * @param cls
     */
    private void checkParams(ClassEntity baseClassEntity, Class<?> cls) {


        //数据完整性校验
        if(baseClassEntity.getParentId()==0L){
            baseClassEntity.setPathId("/0/");
            baseClassEntity.setPathName("/一级分类/");
        }else {
            ClassEntity pClassEntity=this.getById(baseClassEntity.getParentId());
            baseClassEntity.setPathName(pClassEntity.getPathName()+pClassEntity.getClassName()+"/");
            baseClassEntity.setPathId(pClassEntity.getPathId()+pClassEntity.getId()+"/");
        }

        //实体类参数校验
        ValidatorUtils.validateEntity(baseClassEntity, cls);

        //其它业务参数校验

    }

    //获取子节点数据
    private List<TreeVo<ClassEntity>> getChilds(Long uomId){
        List<TreeVo<ClassEntity>> list=new ArrayList<>();
        QueryWrapper query= new QueryWrapper<ClassEntity>();
        query.eq("parent_id",uomId);
        query.eq("is_valid", ValidEnum.YES.getValue());
        List<ClassEntity> listClassEntity=this.list(query);
        for (ClassEntity classEntity:listClassEntity){
            TreeVo<ClassEntity> treeVo=new TreeVo<>();
            treeVo.setId(classEntity.getId());
            treeVo.setTitle(classEntity.getClassName());
            treeVo.setSpread(true);
            treeVo.setChildren(this.getChilds(classEntity.getId()));
            list.add(treeVo);
        }
        return list;
    }


}
